﻿#include "cadre.h"
#include "ui_cadre.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRecord>


CCadre::CCadre(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::CCadre) {
    ui->setupUi(this);

    ui->tb_cadre->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tb_cadre->horizontalHeader()->setStretchLastSection(true);
    ui->tb_cadre->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
    openTable();

}


void CCadre::openTable() {


    model = new QSqlRelationalTableModel(this);

    model->setTable("CadreInfo");
    model->setRelation(22, QSqlRelation("unit", "UnitID", "unitname"));
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);


    if (!(model->select()))//查询数据
    {
        QMessageBox::critical(this, "错误信息",
                              "打开数据表错误,错误信息\n" + model->lastError().text(),
                              QMessageBox::Ok, QMessageBox::NoButton);
        return;
    }

    model->setHeaderData(0, Qt::Horizontal, QObject::tr("姓名"));

    model->setHeaderData(4, Qt::Horizontal, QObject::tr("出生日期"));
    model->setHeaderData(3, Qt::Horizontal, QObject::tr("证件号码"));
    model->setHeaderData(6, Qt::Horizontal, QObject::tr("性别"));
    model->setHeaderData(22, Qt::Horizontal, QObject::tr("单位"));

    ui->tb_cadre->setModel(model);
    ui->tb_cadre->hideColumn(5);
    ui->tb_cadre->hideColumn(8);
    ui->tb_cadre->hideColumn(2);
    ui->tb_cadre->hideColumn(1);
    ui->tb_cadre->hideColumn(7);
    ui->tb_cadre->hideColumn(9);
    ui->tb_cadre->hideColumn(10);
    ui->tb_cadre->hideColumn(11);
    ui->tb_cadre->hideColumn(12);
    ui->tb_cadre->hideColumn(16);
    ui->tb_cadre->hideColumn(17);
    ui->tb_cadre->hideColumn(15);
    ui->tb_cadre->hideColumn(14);
    ui->tb_cadre->hideColumn(13);
    ui->tb_cadre->hideColumn(18);
    ui->tb_cadre->hideColumn(19);
    ui->tb_cadre->hideColumn(20);
    ui->tb_cadre->hideColumn(21);
    ui->tb_cadre->hideColumn(1);
    ui->tb_cadre->hideColumn(25);
    ui->tb_cadre->hideColumn(24);
    ui->tb_cadre->hideColumn(23);
    ui->tb_cadre->hideColumn(26);
    ui->tb_cadre->hideColumn(27);
    int rows = model->rowCount();
    char sex;
    QVariant sex_1 = "女";
    QVariant sex_0 = "男";
    for (int i = 0; i <= rows; i++) {
        QModelIndex index = model->index(i, 6);
        qDebug() << index;
        if (sex == '0') {
            model->setData(index, sex_0, Qt::EditRole);
        } else {
            model->setData(index, sex_1, Qt::EditRole);
        }
    }

}

CCadre::~CCadre() {
    delete ui;
}

void CCadre::on_btn_select_clicked() {

    //数据库查询
    QString value_cadre = ui->lineEdit->text();

    if (value_cadre != NULL) {


        model = new QSqlRelationalTableModel(this);

        model->setTable("CadreInfo");
        model->setFilter("CadreName='" + value_cadre + "'");
        model->setRelation(22, QSqlRelation("unit", "UnitID", "unitname"));
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);


        if (!(model->select()))//查询数据
        {
            QMessageBox::critical(this, "错误信息",
                                  "打开数据表错误,错误信息\n" + model->lastError().text(),
                                  QMessageBox::Ok, QMessageBox::NoButton);
            return;
        }
        model->setHeaderData(0, Qt::Horizontal, QObject::tr("姓名"));

        model->setHeaderData(4, Qt::Horizontal, QObject::tr("出生日期"));
        model->setHeaderData(3, Qt::Horizontal, QObject::tr("证件号码"));
        model->setHeaderData(6, Qt::Horizontal, QObject::tr("性别"));
        model->setHeaderData(22, Qt::Horizontal, QObject::tr("单位"));

        ui->tb_cadre->setModel(model);
        ui->tb_cadre->hideColumn(5);
        ui->tb_cadre->hideColumn(8);
        ui->tb_cadre->hideColumn(2);
        ui->tb_cadre->hideColumn(1);
        ui->tb_cadre->hideColumn(7);
        ui->tb_cadre->hideColumn(9);
        ui->tb_cadre->hideColumn(10);
        ui->tb_cadre->hideColumn(11);
        ui->tb_cadre->hideColumn(12);
        ui->tb_cadre->hideColumn(16);
        ui->tb_cadre->hideColumn(17);
        ui->tb_cadre->hideColumn(15);
        ui->tb_cadre->hideColumn(14);
        ui->tb_cadre->hideColumn(13);
        ui->tb_cadre->hideColumn(18);
        ui->tb_cadre->hideColumn(19);
        ui->tb_cadre->hideColumn(20);
        ui->tb_cadre->hideColumn(21);
        ui->tb_cadre->hideColumn(1);
        ui->tb_cadre->hideColumn(25);
        ui->tb_cadre->hideColumn(24);
        ui->tb_cadre->hideColumn(23);
        ui->tb_cadre->hideColumn(26);
        int rows = model->rowCount();
        char sex;
        QVariant sex_1 = "女";
        QVariant sex_0 = "男";
        for (int i = 0; i <= rows; i++) {
            QModelIndex index = model->index(i, 6);
            qDebug() << index;
            if (sex == '0') {
                model->setData(index, sex_0, Qt::EditRole);
            } else {
                model->setData(index, sex_1, Qt::EditRole);
            }
        }

        ui->tb_cadre->show();


    } else {
        openTable();
    }

}

void CCadre::on_tb_cadre_clicked(const QModelIndex &index) {

}

void CCadre::on_tb_cadre_doubleClicked(const QModelIndex &index)
{
    int currow = ui->tb_cadre->currentIndex().row();
    QString name = model->record(currow).value(0).toString();
    QString idnum = model->record(currow).value(3).toString();
    qDebug() << name << "\n" << idnum;
    emit sendSignal(name, idnum);
    this->close();
}
